 package com.gmrz.uaf.common;
 
 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.name.Names;
 
 public class UAFBaseModule extends AbstractModule
 {
   private static final Logger LOG = LogManager.getLogger(UAFBaseModule.class);
 
   protected Properties moduleProperties = null;
   protected List<String> moduleConfigurations = new ArrayList<String>();
   protected Map<String, String> defaultModuleConfigurations = new HashMap<String,String>();
 
   public UAFBaseModule(Properties moduleProperties) {
     this.moduleProperties = moduleProperties;
   }
 
   protected void configure()
   {
     bindConfigurations();
   }
 
   protected void bindConfigurations() {
     for (String c : this.moduleConfigurations) {
       String value = this.moduleProperties.getProperty(c);
       LOG.debug("Configuration " + c + "=" + value);
       if ((value == null) || (value.isEmpty())) {
         bind(String.class).annotatedWith(Names.named(c)).toProvider(new DefaultConfigProvider(c));
       }
       else
         bind(String.class).annotatedWith(Names.named(c)).toInstance(value);
     }
   }
 
   public class DefaultConfigProvider implements Provider<String>
   {
     final String configName;
 
     DefaultConfigProvider(String c)
     {
       this.configName = c;
     }
 
     public String get()
     {
       if (UAFBaseModule.this.defaultModuleConfigurations.containsKey(this.configName)) {
         return UAFBaseModule.this.defaultModuleConfigurations.get(this.configName);
       }
        if(this.configName.equals(Constants.UAP_APP_ID)){
         return "";
        }
       String errorMesage = "Configuring " + this.configName + " is mandatory. There is no default value for this configuration.";
       UAFBaseModule.LOG.error(errorMesage);
       throw new UAFRuntimeException(errorMesage);
     }
   }
 }